package ch05;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

@SuppressWarnings("serial")
public class FunctionPrinter extends JComponent{	
	public static final int WIDTH = 400;
	public static final int HEIGHT = 300;
	
	private final int MARGIN = 20;
	
	private final int XMIN = MARGIN;
	private final int XMAX = WIDTH - MARGIN;
	
	private final int STEPS = 100;
	private final int STEPSIZE = (XMAX - XMIN) / STEPS;	
	
	public FunctionPrinter(){
				
	}
	
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		
		for(int i = XMIN; i <= XMAX - STEPSIZE; i += STEPSIZE)
		{
			g2.drawLine(i, (int)f(i), i + STEPSIZE, (int)f(i + STEPSIZE));
		}
	}
	
	public double f(double x)
	{
		return 0.00005*Math.pow(x, 3) - 0.03*Math.pow(x, 2) + 4*x + 200;
	}
}
